package com.hup.minicontroller.server.auth;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.util.Log;

import com.hup.minicontroller.server.ServerApplication;
import com.hup.minicontroller.server.mvp.IScreenImageService;
import com.hup.minicontroller.server.screenImageService.MediaProjectionScreenService;

/**
 * {@link MediaProjectionScreenService}的[录屏权限]申请工具类
 *
 * @author hugan
 * @date 2021/5/8
 */
public abstract class MediaProjectionAuthForSIS {
    private static final String TAG = MediaProjectionAuthForSIS.class.getSimpleName();

    public static int REQUEST_CODE_MEDIA_PROJECTION = 24586;
    private static AuthRejectCounter rejectCounter = new AuthRejectCounter();

    /**
     * 使用Activity请求权限
     *
     * @param activity 必须重写handleActivityResult并调用 {@link #handleActivityResult(int, int, Intent, Activity)}
     */
    public static void acquireMediaProjectionForService(Activity activity) {
        Log.i(TAG, "acquireMediaProjectionForService: 开始申请权限");
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        if (mediaProjectionManager == null) {
            Log.e(TAG, "acquireMediaProjectionForService: mediaProjectionManager==null???");
            ServerApplication.getBean(IScreenImageService.class).acquireMediaProjectionResult(null);
            return;
        }
        activity.startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE_MEDIA_PROJECTION);
    }

    /**
     * 处理权限申请的返回结果
     *
     * @return 是否处理消息, true-已处理,是本类的请求结果;false-没处理,不是本类的请求结果
     */
    public static boolean handleActivityResult(int requestCode, int resultCode, Intent data, Activity activity) {
        if (requestCode != REQUEST_CODE_MEDIA_PROJECTION) return false;
        MediaProjection mediaProjection = null;
        if (resultCode == Activity.RESULT_OK) {
            try {
                MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
                 /*
                异常:Media projections require a foreground service of type ServiceInfo.FOREGROUN
                    方案1:gradle的targetSdkVersion 要≤28
                    方案2:注册前台服务 android:foregroundServiceType="mediaProjection"
                 */
                mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
            } catch (Exception e) {
                Log.e(TAG, "handleActivityResult: ", e);
            }
        } else {
            Log.e(TAG, "handleActivityResult: 用户拒绝录屏权限");
            //拒绝后重复申请权限
            if (rejectCounter.count < 1) {
                rejectCounter.count++;
                acquireMediaProjectionForService(activity);
            } else {
                ServerApplication.getBean(IScreenImageService.class).acquireMediaProjectionResult(null);
            }
            return true;
        }
        ServerApplication.getBean(IScreenImageService.class).acquireMediaProjectionResult(mediaProjection);
        return true;
    }

}
